import type { VbenFormSchema } from '#/adapter/form'

import type { Ref } from 'vue'
import { computed } from 'vue'

import type { ExtendedFormApi } from '@vben/common-ui'
import { $t } from '@vben/locales'
import { isEmpty } from '@vben/utils'

import SysRolesApi from '#/api/system/roles'
import { formValidMobile } from '#/utils/formValidator'

export function getAddEditFormSchema(opts?: {
    formApi?: ExtendedFormApi
    rowId: Ref<null | number>
}) {
    const { rowId } = opts || {}
    const isEdit = computed(() => !isEmpty(rowId?.value))
    return [
        {
            fieldName: 'realname',
            label: $t('page.system.children.user.children.realname'),
            defaultValue: '',
            component: 'Input'
        },
        {
            fieldName: 'nickname',
            label: $t('page.system.children.user.children.nickname'),
            component: 'Input',
            defaultValue: '',
            rules: 'required'
        },
        {
            label: $t('page.system.children.user.children.roles'),
            fieldName: 'roleIds',
            component: 'TableSelect',
            componentProps: {
                options: [],
                formOptions: {
                    schema: [
                        {
                            component: 'Input',
                            defaultValue: '',
                            fieldName: 'name',
                            label: $t('page.system.children.role.children.name'),
                            formItemClass: 'col-span-2'
                        }
                    ]
                },
                columns: [
                    {
                        title: $t('page.system.children.role.children.name'),
                        field: 'name',
                        width: 120
                    },
                    {
                        title: $t('common.description'),
                        field: 'note'
                    }
                ],
                api: SysRolesApi.inst.getPageList
            }
        },
        {
            fieldName: 'username',
            label: $t('page.system.children.user.children.username'),
            component: 'Input',
            defaultValue: '',
            rules: 'required'
        },
        {
            fieldName: 'password',
            label: $t('page.system.children.user.children.password'),
            component: 'InputPassword',
            defaultValue: '',
            rules: 'required',
            dependencies: {
                triggerFields: ['password'],
                if: () => {
                    // 编辑状态不显示
                    return !isEdit.value
                }
            }
        },
        {
            fieldName: 'mobile',
            label: $t('page.system.children.user.children.mobile'),
            component: 'Input',
            defaultValue: '',
            rules: formValidMobile(),
            componentProps: {
                maxlength: 11
            }
        },
        {
            fieldName: 'gender',
            label: $t('options.gender.title'),
            component: 'RadioGroup',
            defaultValue: 1,
            componentProps: {
                options: [
                    {
                        label: $t('options.gender.children.male'),
                        value: 1
                    },
                    {
                        label: $t('options.gender.children.female'),
                        value: 2
                    }
                ]
            }
        },
        {
            fieldName: 'status',
            label: $t('options.status.title'),
            component: 'Switch',
            componentProps: {
                checkedValue: 1,
                unCheckedValue: 2,
                checkedChildren: $t('options.status.children.normal'),
                unCheckedChildren: $t('options.status.children.disabled')
            },
            defaultValue: 1
        }
    ] as VbenFormSchema[]
}
